import httplib2
import json
import sqlite3
from urllib import parse
import gl
from pkg.db import db


def mk_db():
    conn = sqlite3.connect(gl.g_db_name)
    cur = conn.cursor()
    cur.execute(gl.g_db_user_build)
    db.build_user_msg_tab()
    conn.execute(gl.g_db_voice_build)


def get_baidu_voice_tok(client_id='0UU1AY26VGStO3z6ZYa65w28',client_secret='f6123dff5a3bf1d23e99ff50fcc6fd33'):
    http=httplib2.Http(gl.g_http_catch_dir)
    data = {
        'grant_type': 'client_credentials',
        'client_id': client_id,
        'client_secret': client_secret
    }
    try:
        res, content = http.request('https://openapi.baidu.com/oauth/2.0/token', 'POST', parse.urlencode(data))
        if not (res['content-type'] == 'application/json' and res.status == 200):
            return False
        js = json.loads(content.decode())
        gl.g_token=js['access_token']
        return True
    except Exception as Err:
        print(Err)
        return False
